unit clsTipoDocumento;

interface

uses Dm, dbtables, biblio;

type
  TTipoDocumento = class(TObject)
  private
    Tipd_Codigo: string;
  public
    Tipd_Descricao: string;
    function Gravar(p_Tipd_Codigo: string): Byte;
    function Recuperar(p_Tipd_Codigo: string): Byte;
    function Excluir(p_Tipd_Codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
  end;

implementation

function TTipoDocumento.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM TipoDocumento where Tipd_Codigo=:k_Tipd_Codigo');
  tq.parambyname('k_Tipd_Codigo').asstring := p_Tipd_Codigo;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO TipoDocumento VALUES(:k_Tipd_Codigo,:k_Tipd_Descricao)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE TipoDocumento SET Tipd_Descricao=:k_Tipd_Descricao WHERE Tipd_Codigo=:k_Tipd_Codigo');
  end;
  tq.parambyname('k_Tipd_Codigo').asstring := p_Tipd_Codigo;
  tq.parambyname('k_Tipd_Descricao').asstring := Tipd_Descricao;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TTipoDocumento.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM TipoDocumento where Tipd_Codigo=:k_Tipd_Codigo');
  tq.Params[0].asstring := p_Tipd_Codigo;
  tq.open;
  Tipd_Codigo := p_Tipd_Codigo;
  if tq.eof then begin
    Tipd_Descricao := '';
    Result := 0;
  end else begin
    Tipd_Descricao := tq.fieldbyname('Tipd_Descricao').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TTipoDocumento.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM TipoDocumento WHERE Tipd_Codigo=:k_Tipd_Codigo');
  tq.parambyname('k_Tipd_Codigo').asstring := p_Tipd_Codigo;
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TTipoDocumento.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select distinct * from TipoDocumento where Tipd_Descricao>=:k_Tipd_Descricao order by Tipd_codigo');
  p_q.parambyname('k_Tipd_Descricao').asstring := p_nome;
end;

end.
